const isEmpty = (value) => {
    if (value === undefined || !value.toString() || value.toString() === undefined) {
        return true;
    }

    return typeof value === "string" && value.trim() === "";
};

export default {
    // 非空验证
    NotEmpty: {
        required: true,
        validator: (rule, value, callback) => {
            if (isEmpty(value)) {
                return callback(new Error("内容不能为空"));
            }

            callback();
        },
        trigger: "blur"
    },
    // 序列号
    NotString: {
        required: true,
        validator: (rule, value, callback) => {
            const notString = /^[\u4e00-\u9fa5]/;
            if (isEmpty(value)) {
                return callback(new Error("内容不能为空"));
            };
            if ((notString.test(value))) {
                callback(new Error("请输入合法的序列号"));
            }
            callback();
        },
        trigger: "blur"
    },
    // 编号规则（无中文）
    NotChinese: {
        required: true,
        validator: (rule, value, callback) => {
            const NotChinese = /^[A-Za-z0-9]+$/;
            if (isEmpty(value)) {
                return callback(new Error("内容不能为空"));
            };
            if (!(NotChinese.test(value))) {
                callback(new Error("不能为汉字，请输入字母或数字"));
            }
            callback();
        },
        trigger: "blur"
    },
    // 必须选择
    SelectRule: {
        required: true,
        validator: (rule, value, callback) => {
            if (isEmpty(value)) {
                return callback(new Error("必须选择其中一项"));
            }

            callback();
        },
        trigger: "change"
    },
    // 必须是数字
    MustBeNumber: {
        required: true,
        validator: (rule, value, callback) => {
            if (isEmpty(value)) {
                return callback(new Error("内容不能为空"));
            }

            if (!(/^[0-9]*$/.test(value))) {
                callback(new Error("请输入数字"));
            }

            callback();
        },
        trigger: "blur"
    },
    // 日期规则
    DateRule: {
        required: true,
        validator: (rule, value, callback) => {
            if (!value || value === null || value === undefined) {
                callback(new Error("请选择日期"));
            }

            callback();
        },
        trigger: "change"
    },
    // mac地址规则
    MacRule: {
        required: true,
        validator: (rule, value, callback) => {
            const macRegularExpression = /[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}/;

            if (isEmpty(value)) {
                return callback(new Error("内容不能为空"));
            }

            if (!macRegularExpression.test(value)) {
                callback(new Error("请输入合法的MAC地址"));
            }

            callback();
        },
        trigger: "blur"
    },
    // IP地址规则
    IpRule: {
        required: true,
        validator: (rule, value, callback) => {
            const ipRegularExpression = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;

            if (isEmpty(value)) {
                return callback(new Error("内容不能为空"));
            }

            if (!ipRegularExpression.test(value)) {
                callback(new Error("请输入合法的ip地址"));
            }

            callback();
        },
        trigger: "blur"
    },
    // 经度规则
    LongitudeRule: {
        required: true,
        validator: (rule, value, callback) => {
            // const ipRegularExpression = /^-?(?:(?:180(?:\.0{1,8})?)|(?:(?:(?:1[0-7]\d)|(?:[1-9]?\d))(?:\.\d{1,8})?))$/;
            // const ipRegularExpression = /^(((\d|[1-9]\d|1[1-7]\d|0)\.\d{0,9})|(\d|[1-9]\d|1[1-7]\d|0{1,3})|180\.0{0,9}|180)$/;
            const ipRegularExpression = /^-?((0|1?[0-7]?[0-9]?)(([.][0-9]{1,9})?)|180(([.][0]{1,9})?))$/;

            if (isEmpty(value)) {
                return callback(new Error("内容不能为空"));
            }

            if (!ipRegularExpression.test(value)) {
                callback(new Error("-180~180<10位小数"));
            }

            callback();
        },
        trigger: "blur"
    },
    // 纬度规则
    LatitudeRule: {
        required: true,
        validator: (rule, value, callback) => {
            // const ipRegularExpression = /^-?(?:90(?:\.0{1,6})?|(?:[1-8]?\d(?:\.\d{1,6})?))$/;
            const ipRegularExpression = /^-?((0|[1-8]?[0-9]?)(([.][0-9]{1,9})?)|90(([.][0]{1,9})?))$/;

            if (isEmpty(value)) {
                return callback(new Error("内容不能为空"));
            }

            if (!ipRegularExpression.test(value)) {
                callback(new Error("-90~90<10位小数"));
            }

            callback();
        },
        trigger: "blur"
    },
    // 手机号码规则
    phoneRule: {
        required: true,
        validator: (rule, value, callback) => {
            const phoneRegularExpression = /^[1][1-9][0-9]{9}$/;
            if (isEmpty(value)) {
                return callback(new Error("内容不能为空"));
            }

            if (!phoneRegularExpression.test(value)) {
                callback(new Error("请输入合法的手机号码"));
            }

            callback();
        },
        trigger: "blur"
    },
    // 手机号和固话码规则（无非空验证）
    phoneSimplePhone: {
        required: true,
        validator: (rule, value, callback) => {
            const phonePhoneRegularExpression = /^[1][1-9][0-9]{9}$/;
            const phonePhoneRegularExpression2 = /^0\d{2,3}-\d{7,8}(-\d{1,6})?$/;
            if (!phonePhoneRegularExpression.test(value) && !phonePhoneRegularExpression2.test(value) && !(value === undefined || !value.toString() || value.toString() === undefined || value === "——")) {
                callback(new Error("请输入合法的手机号码或者(区号-固话)"));
            }

            callback();
        },
        trigger: "blur"
    },
    // 手机号和固话码规则
    phoneRuleAndPhone: {
        required: true,
        validator: (rule, value, callback) => {
            const phonePhoneRegularExpression = /^[1][1-9][0-9]{9}$/;
            const phonePhoneRegularExpression2 = /^0\d{2,3}-\d{7,8}(-\d{1,6})?$/;
            if (isEmpty(value)) {
                return callback(new Error("内容不能为空"));
            }

            if (!phonePhoneRegularExpression.test(value) && !phonePhoneRegularExpression2.test(value)) {
                callback(new Error("请输入合法的手机号码或者(区号-固话)"));
            }

            callback();
        },
        trigger: "blur"
    }
};
